#include <gl/glut.h>
#include <math.h>
#include <cstdio>


GLsizei	winWidth = 500, winHeight = 500;

const double TWO_PI = 6.2831853;

GLuint regHex[3];
static GLfloat theta_integrated = 0.0;


class scrPt
{
public:
	GLint x, y;
protected:
private:
};

void DrawHexagons (void)
{
	scrPt hexVertex;
	GLdouble hexTheta;
	GLint k;


	regHex[0] = glGenLists(1);
	glNewList(regHex[0], GL_COMPILE);
	{
		glColor3f(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
		{
			for (k=0; k<6; k++)
			{
				hexTheta = TWO_PI * k / 6;
				hexVertex.x = 150 + 100 * cos(hexTheta);
				hexVertex.y = 0 + 100 * sin(hexTheta);
				glVertex2i(hexVertex.x, hexVertex.y);
			}
		}
		glEnd();
	}
	glEndList();


	regHex[1] = glGenLists(1);
	glNewList(regHex[1], GL_COMPILE);
	{
		glColor3f(0.0, 1.0, 0.0);
		glBegin(GL_POLYGON);
		{
			for (k=0; k<6; k++)
			{
				hexTheta = TWO_PI * k / 6;
				hexVertex.x = 400 + 100 * cos(hexTheta);
				hexVertex.y = 0 + 100 * sin(hexTheta);
				glVertex2i(hexVertex.x, hexVertex.y);
			}
		}
		glEnd();
	}
	glEndList();


// 	regHex[2] = glGenLists(1);
// 	glNewList(regHex[2], GL_COMPILE);
// 	{
// 		glColor3f(0.0, 0.0, 1.0);
// 		glBegin(GL_POLYGON);
// 		{
// 			for (k=0; k<6; k++)
// 			{
// 				hexTheta = TWO_PI * k / 6;
// 				hexVertex.x = 650 + 100 * cos(hexTheta);
// 				hexVertex.y = 0 + 100 * sin(hexTheta);
// 				glVertex2i(hexVertex.x, hexVertex.y);
// 			}
// 		}
// 		glEnd();
// 	}
// 	glEndList();

}

void init (void)
{

	glClearColor(1.0, 1.0, 1.0, 0.0);

	DrawHexagons();
}

void displayHex (void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();
	glRotatef(theta_integrated, 0.0, 0.0, 1.0);
	glCallList(regHex[0]);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(0.0, 50*sin(theta_integrated), 0.0);
	glRotatef(theta_integrated, 0.0, 0.0, 1.0);
	glCallList(regHex[1]);
	glPopMatrix();

	glutSwapBuffers();


	glFlush();
}

void IncreaseTheta (void)
{
	theta_integrated += 3.0;
	if (theta_integrated > 360.0)
		theta_integrated -= 360.0;

	glutPostRedisplay();
}

void displayWirePolyhedra (void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(0.0, 0.0, 1.0);


	gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);


	glScalef(1.5, 5.0, 1.0);
	glutWireCube(1.0);

	glTranslatef(3.0, 0.0, 0.0);
	glutWireTeapot(1.0);

	glFlush();
}


void wireQuadSurfs (void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(0.0, 0.0, 1.0);

	gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);



	glPushMatrix();
	glTranslatef(1.0, 1.0, 0.0);
	glutWireSphere(0.75, 15, 15);
	glPopMatrix();


	glPushMatrix();
	glTranslatef(1.0, -0.5, 0.5);
	glutWireCone(0.7, 2.0, 7, 6);
	glPopMatrix();

	GLUquadricObj * cylinder;
	glPushMatrix();
	glTranslatef(0.0, 1.2, 0.8);
	cylinder = gluNewQuadric();
	gluQuadricDrawStyle(cylinder, GLU_LINE);
	gluCylinder(cylinder, 0.6, 0.6, 1.5, 26, 4);
	glPopMatrix();

	glFlush();
}




void winReshapeFcn (GLint newWidth, GLint newHeight)
{
	glViewport(0, 0, newWidth, newHeight);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-1000.0, 1000.0, -1000.0, 1000.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClear(GL_COLOR_BUFFER_BIT);
}


void mouseFcn (GLint button, GLint action, GLint x, GLint y)
{
	switch (button)
	{
	case GLUT_LEFT_BUTTON :
		if (action == GLUT_DOWN)
			glutIdleFunc(IncreaseTheta);
		break;
	case GLUT_RIGHT_BUTTON :
		if (action == GLUT_DOWN)
			glutIdleFunc(NULL);
		break;
	default:
		break;
	}
}


void main (int argc, char** argv)
{
	glutInit (&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(winWidth, winHeight);
	glutCreateWindow("wire-frame polyhedra");


	init();
	glutDisplayFunc(displayHex);
	glutReshapeFunc(winReshapeFcn);
	glutMouseFunc(mouseFcn);

	glutMainLoop();
}


